import { createRouter, createWebHashHistory } from "vue-router"

// 获取主页部分
import layout from "@/layout"
// user
import UserManageRouter from './modules/UserManage'
// 角色
import RoleListRouter from './modules/RoleList'
// 权限
import PermissionListRouter from './modules/PermissionList'
// 文章
import ArticleRouter from './modules/Article'
import ArticleCreaterRouter from './modules/ArticleCreate'
import store from '@/store'

/**
 * 私有路由表
 */
export const privateRoutes = [
  UserManageRouter,
  RoleListRouter,
  PermissionListRouter,
  ArticleRouter,
  ArticleCreaterRouter
]

// 设置公开路由表
export const publicRoutes = [
  // 登录页面
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/login")
  },
  //登陆后进入的主页
  {
    path: "/",
    component: () => import("@/layout"),
    redirect: '/profile',
    children: [
      // 个人中心
      {
        path: "/profile",
        name: "profile",
        component: () =>
          import(/* webpackChunkName: "profile" */ "@/views/profile/index"),
        meta: {
          title: "profile",
          icon: "el-icon-user"
        }
      },
      // 404
      {
        path: "/404",
        name: "404",
        component: () =>
          import(/* webpackChunkName: "error-page" */ "@/views/error-page/404")
      },
      // 401
      {
        path: "/401",
        name: "401",
        component: () =>
          import(/* webpackChunkName: "error-page" */ "@/views/error-page/401")
      }
    ]
  }
]

// 初始化路由表
export function resetRouter() {
  if (
    store.getters.userInfo &&
    store.getters.userInfo.permission &&
    store.getters.userInfo.permission.menus
  ) {
    const menus = store.getters.userInfo.permission.menus
    menus.forEach((menu) => {
      router.removeRoute(menu)
    })
  }
}

const router = createRouter({
  history: createWebHashHistory(),
  // 抛出公共路由
  routes: [...publicRoutes,...privateRoutes]
})

export default router
